/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */
package com.lxk.pay.wallet;

import cn.hutool.core.util.StrUtil;
import com.lxk.pay.PayService;
import com.lxk.pay.model.*;
import com.lxk.service.core.Result;
import com.lxk.service.finance.api.WalletServiceApi;
import com.lxk.service.finance.enums.AccountTypeEnum;
import com.lxk.service.finance.request.UpdateAccountParam;
import com.lxk.service.pay.constants.PayServiceBeanConstants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author huahua
 * 2024/8/22
 */
@Slf4j
@RequiredArgsConstructor
@Service(PayServiceBeanConstants.WALLET_PAY_SERVICE)
public class WalletPayService implements PayService {

    private final WalletServiceApi walletServiceApi;

    @Override
    public PayResultModel pay(PayRequestModel payRequestModel) {
        PayResultModel payResultModel = new PayResultModel();
        if (payRequestModel.getUserId() == null) {
            payResultModel.setSuccess(false);
            payResultModel.setMsg("用户id不能为空");
            return payResultModel;
        }
        if (payRequestModel.getTotalFee() == null) {
            payResultModel.setSuccess(false);
            payResultModel.setMsg("支付金额不能为空");
            return payResultModel;
        }
        if (StrUtil.isBlank(payRequestModel.getOutTradeNo())) {
            payResultModel.setSuccess(false);
            payResultModel.setMsg("商户的订单号不能为空");
            return payResultModel;
        }
        try {
            UpdateAccountParam param = new UpdateAccountParam();
            param.setUserId(payRequestModel.getUserId());
            param.setAmount(payRequestModel.getTotalFee());
            param.setOrderSn(payRequestModel.getOutTradeNo());
            param.setAccountType(AccountTypeEnum.MONEY.getCode());
            param.setExtraInfo(payRequestModel.getBody());
            Result<Boolean> res = walletServiceApi.reduce(param);
            payResultModel.setSuccess(res.getData());
            payResultModel.setMsg(payResultModel.getMsg());
            return payResultModel;
        } catch (Exception e) {
            log.error("余额支付错误", e);
            payResultModel.setSuccess(false);
            payResultModel.setMsg(e.getMessage());
        }
        return payResultModel;
    }

    @Override
    public PayNotifyResultModel payNotify(Map<String, String> params, String appId) {
        PayNotifyResultModel payNotifyResultModel = new PayNotifyResultModel();
        log.info("支付通知={}", params);

        return payNotifyResultModel;
    }

    @Override
    public RefundResultModel refund(PayRefundModel payRequestModel) {
        RefundResultModel resultModel = new RefundResultModel();
        if (payRequestModel.getUserId() == null) {
            resultModel.setSuccess(false);
            resultModel.setMsg("用户id不能为空");
            return resultModel;
        }
        if (payRequestModel.getRefundFee() == null) {
            resultModel.setSuccess(false);
            resultModel.setMsg("退款金额不能为空");
            return resultModel;
        }
        if (StrUtil.isBlank(payRequestModel.getOutTradeNo())) {
            resultModel.setSuccess(false);
            resultModel.setMsg("商户的订单号不能为空");
            return resultModel;
        }
        try {
            UpdateAccountParam param = new UpdateAccountParam();
            param.setUserId(payRequestModel.getUserId());
            param.setAmount(payRequestModel.getRefundFee());
            param.setOrderSn(payRequestModel.getOutTradeNo());
            param.setAccountType(AccountTypeEnum.MONEY.getCode());
            param.setExtraInfo("正常退款");
            Result<Boolean> res = walletServiceApi.add(param);
            resultModel.setSuccess(res.getData());
            resultModel.setMsg(resultModel.getMsg());
            return resultModel;
        } catch (Exception e) {
            log.error("余额退款错误", e);
            resultModel.setSuccess(false);
            resultModel.setMsg(e.getMessage());
        }
        return resultModel;
    }

    /**
     * 订单支付状态查询
     *
     * @param requestModel
     * @return
     */
    @Override
    public OrderQueryResultModel orderQuery(OrderQueryRequestModel requestModel) {
        return null;
    }

}
